#require "rubygems"
#require "action_mailer"
#
#module ::Wondercap
#  module MailNotifiers
#    class Mailer < ActionMailer::Base
#      def notification(from, to, subject, body)
#        self.from from
#        self.recipients to
#        self.subject subject
#        self.body body
#      end
#    end      
#  end
#end
#
#extension :task_notification => :mail do
#  task(:_notify) do
#    subject = ERB.new(fetch(:mail_notification_subject, _default_subject), nil, "%<>").result(binding)
#    body = ERB.new(fetch(:mail_notification_body, _default_body), nil, "%<>").result(binding)
#    Wondercap::MailNotifiers::Mailer.deliver_notification(fetch(:mail_notification_from, mail_notification_to), mail_notification_to, subject, body)
#  end
#  
#  set(:installed_revision_description) do
#    installed_revision_deployment ? installed_revision_deployment.real_revision : "not found"
#  end  
#  
#  set :_default_task_per_configuration_subject, %q{<%= task_name.capitalize %> <%= status.to_s.upcase %> (<%= area.name %>): <%= project_name %>}  
#  set :_default_task_per_configuration_body, <<-'EOF'
#Name: <%= project_name %>
#Task: <%= task_name %>
#Area: <%= area.name %>
#Revision in area: <%= installed_revision_description %>
#% if outer_configuration.exists? :responsible_user
#Responsible user: <%= outer_configuration.responsible_user %>
#% end
#Status: <%= status %>
#
#% if exists?(:error)
#Error: <%= error %>
#<%= error.backtrace.join("\n\t") %>
#% end
#Log:
#<%= logger_output(Capistrano::Logger::INFO) %>      
#EOF
#
#  set :_default_task_summary_subject, %q{<%= task_name.capitalize %> <%= status.to_s.upcase %> (<%= area.name %>): <%= outer_configuration.names.sort.join(",") %>}
#  set :_default_task_summary_body, <<-'EOF'
#Task: <%= task_name %>
#% if outer_configuration.exists? :responsible_user
#Responsible user: <%= outer_configuration.responsible_user %>
#% end
#Status: <%= status.to_s.upcase %>
#Destination area: <%= area.name %>
#Supplied names (after filtering): <%= outer_configuration.names.join(",") %>
#
#% if exists?(:error) && error.respond_to?(:errors)
#Names ([S] means succeeded):
#<% outer_configuration.names.each do |name| %>
# * <%= name %><%= error.errors.find { |conf, e| conf.project_name == name } ? "" : " [S]"%>
#% end
#% elsif exists?(:error)
#Error: <%= error %>
#<%= error.backtrace.join("\n\t") %>
#
#% else
#Successful names:
#% outer_configuration.names.each do |name|
# * <%= name %>
#%end
#%end
#
#% case fetch(:mail_notification_log_mode, :errors_only)
#% when :total
#Log:
#<%= outer_configuration.logger_output(Capistrano::Logger::INFO) %>      
#% when :errors_only
#% if exists?(:error) && error.respond_to?(:errors)
#<% outer_configuration.names.each do |name| %>
#% conf_error_pair = error.errors.find { |conf, e| conf.project_name == name }; next unless conf_error_pair; conf_error = conf_error_pair[1]
#* <%= name %>
#Error: <%= conf_error %>
#<%= conf_error.backtrace.join("\n\t") %>
#
#Log:
#<%= outer_configuration.mc.find_configuration(name).logger_output(Capistrano::Logger::INFO) %>
#--------------------------------------------------------------------------------
#
#% end
#% end
#% end
#EOF
#
#  task :notify_task_success do
#    execute_previous
#    set :status, :success
#    set :_default_subject, _default_task_per_configuration_subject
#    set :_default_body, _default_task_per_configuration_body
#    _notify
#  end
#  
#  task :notify_task_failure do
#    execute_previous
#    set :status, :failure
#    set :_default_subject, _default_task_per_configuration_subject
#    set :_default_body, _default_task_per_configuration_body
#    _notify    
#  end
#  
#  task :notify_task_success_summary do
#    execute_previous
#    set :status, :success
#    set :_default_subject, _default_task_summary_subject
#    set :_default_body, _default_task_summary_body
#    _notify    
#  end
#  
#  task :notify_task_failure_summary do
#    execute_previous
#    set :status, :failure
#    set :_default_subject, _default_task_summary_subject
#    set :_default_body, _default_task_summary_body
#    _notify    
#  end
#  
#end
#
#extension :deployment_notification => :mail do
#  
#  set :_default_deployment_summary_body, <<-'EOF'
#% if outer_configuration.exists? :responsible_user
#Responsible user: <%= outer_configuration.responsible_user %>
#% end
#Status: <%= status.to_s.upcase %>
#Destination area: <%= area.name %>
#Supplied names (after filtering): <%= outer_configuration.names.join(",") %>
#Ignore erros: <%= outer_configuration.fetch(:ignore_errors, false) %>
#
#% if exists?(:error) && error.respond_to?(:errors)
#Deployments ([F] means failed):
#<% outer_configuration.names.each do |name| %>
# * <%= "#{name}:#{outer_configuration.mc.find_configuration(name).real_revision}" %><%= error.errors.has_key?(name) ? " [F]" : ""%>
#% end
#% elsif exists?(:error)
#Error: <%= error %>
#<%= error.backtrace.join("\n\t") %>
#
#% error
#Successful deployments:
#% outer_configuration.names.each do |name|
# * <%= name %>
#%end
#%end
#
#Log:
#<%= outer_configuration.logger_output(Capistrano::Logger::INFO) %>   
#EOF
#  
#  set :_default_deployment_per_configuration_subject, %q{<%= area.name.to_s == "local" ? "Local" : "Remote" %> deployment <%= status.to_s.upcase %> (<%= area.name %>): <%= project_name %>:<%= real_revision %>}
#  set :_default_deployment_summary_subject, %q{<%= area.name.to_s == "local" ? "Local" : "Remote" %> deployment <%= status.to_s.upcase %> (<%= area.name %>): <%= outer_configuration.names.map { |name| "#{name}:" + outer_configuration.mc.get_configuration(name).real_revision.to_s }.sort.join(",") %>}
#  
#  task(:_notify) do
#    subject = ERB.new(fetch(:mail_notification_subject, _default_subject), nil, "%<>").result(binding)
#    body = ERB.new(fetch(:mail_notification_body, _default_body), nil, "%<>").result(binding)
#    Wondercap::MailNotifiers::Mailer.deliver_notification(fetch(:mail_notification_from, mail_notification_to), mail_notification_to, subject, body)
#  end
#  
#  task :notify_deployment_success do
#    execute_previous
#  end
#
#  task :notify_deployment_failure do
#    execute_previous
#  end
#
#  task :notify_deployment_success_summary do
#    execute_previous
#    set :status, :success
#    set :_default_subject, _default_deployment_summary_subject
#    set :_default_body, _default_deployment_summary_body
#    _notify    
#  end
#
#  task :notify_deployment_failure_summary do
#    execute_previous
#    set :status, :failure
#    set :_default_subject, _default_deployment_summary_subject
#    set :_default_body, _default_deployment_summary_body
#    _notify    
#  end
#  
#end
#
#extension :notification => :mail do
#  use :task_notification => :mail
#  use :deployment_notification => :mail
#end
